package ruifeng.zcj.happymahjony.activity.sub;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;

import java.io.File;
import java.io.FileOutputStream;

import ruifeng.zcj.happymahjony.AbJsonUtil;
import ruifeng.zcj.happymahjony.FragmentArgs;
import ruifeng.zcj.happymahjony.R;
import ruifeng.zcj.happymahjony.Result;
import ruifeng.zcj.happymahjony.ToActivityUtil;
import ruifeng.zcj.happymahjony.activity.base.BaseActivity;
import ruifeng.zcj.happymahjony.application.App;
import ruifeng.zcj.happymahjony.manager.ActivityManager;
import ruifeng.zcj.happymahjony.util.AppUtil;
import ruifeng.zcj.happymahjony.util.CacheApi;
import ruifeng.zcj.happymahjony.util.Des3;
import ruifeng.zcj.happymahjony.util.DesUtil;
import ruifeng.zcj.happymahjony.util.NetUtils;
import ruifeng.zcj.happymahjony.util.SPUtils;

/**
 * 首页(登录页)<br/>
 */
public class MainActivity extends BaseActivity {

    private ImageView iv_login;

    protected void onCreate(Bundle savedInstanceState) {
        ActivityManager.getInstance().pushActivity(this);
        super.onCreate(savedInstanceState);
        setFullScreen();
        setContentView(R.layout.activity_main);


        initView();
        initData();
//        test();

    }


    private void initView() {
        iv_login = (ImageView) findViewById(R.id.iv_login);
    }

    private void initData() {
        String userMessage = (String) SPUtils.get(this, SPUtils.USER_INFO, null);
        if (!TextUtils.isEmpty(userMessage)) {
            showLongToast("正在登录");
            sendhttps(userMessage);
        }
    }

    private void test() {
        byte[] data = new byte[]{1, 2, 3};
        String result_1 = Base64.encodeToString(data, Base64.NO_WRAP);
        String result_2 = Base64.encodeToString(data, Base64.DEFAULT);
        String result_3 = ruifeng.zcj.happymahjony.util.Base64.encode(data);
        String result_4 = com.squareup.okhttp.internal.Base64.encode(data);
        Log.e("zcj", "result:" + result_1);
        Log.e("zcj", "result:" + result_2);
        Log.e("zcj", "result:" + result_3);
        Log.e("zcj", "result:" + result_4);

        try {
            String result_5 = DesUtil.encrypt("123", "01234567");
            Log.e("zcj", "result_5:" + result_5);

            String result_6 = Des3.encode("123");
            Log.e("zcj", "result_6:" + result_6);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_login:
                doClickLoginView();
                break;
        }
    }

    private void doClickLoginView() {
        String userMessage = (String) SPUtils.get(this, SPUtils.USER_INFO, null);
        if (!TextUtils.isEmpty(userMessage)) {
            showLongToast("正在登录");
            sendhttps(userMessage);
        } else {
            requestWX();
        }
    }

    /**
     * 请求微信授权并登录<br/>
     */
    private void requestWX() {
        IWXAPI mWXAPI = App.getInstance().getWXAPI();
        if (!mWXAPI.isWXAppInstalled()) {
            showLongToast("未安装微信客户端");
            return;
        }
        showLongToast("请稍等");
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "ruifeng.zcj.happymahjony";
        mWXAPI.sendReq(req);
    }

    /**
     * 微信登录<br/>
     */
    private void sendhttps(String tokenBean) {

        String url = "http://mj.lcc.so/wxlogin";
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("user_message", AppUtil.getUserMessage(tokenBean,this));

        //showToast(""+AppUtil.getUserMessage(tokenBean,this));
       // Log.e("zcj",""+AppUtil.getUserMessage(tokenBean,this));
        httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {

            public void onFailure(HttpException arg0, String arg1) {
                //  showToast(arg1);
            }

            public void onSuccess(ResponseInfo<String> responseInfo) {
             //   showToast("登录返回信息："+responseInfo.result);
              //  Log.e("zcj",""+responseInfo.result);
                Result result = (Result) AbJsonUtil.fromJson(responseInfo.result, Result.class);
                if (result.getStatus() == 1) {
                    FragmentArgs args = new FragmentArgs();
                    args.add("result", result);
                    ToActivityUtil.toNextActivityAndFinish(MainActivity.this, MainCordovaActivity.class, "key", args);
                } else {
                    showToast("登录失败，请稍后再试");
                }
                return;
            }
        });
    }


}
